package com.ny.controller;


import com.ny.entity.Classrooms;
import com.ny.entity.Reservations;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private ReservationService reservationService;

    //用户登入查询所以教室
    @RequestMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model, HttpSession session) {
        Users user = userService.login(username, password);
        session.setAttribute("user", user);
        if (user!= null) {
            System.out.println(user);
            List<Classrooms> classroomsList = classroomService.findAllClassrooms();
            System.out.println(classroomsList);
            model.addAttribute("classroomList", classroomsList);
//            model.addAttribute("user", user);
            return "classroom";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }


//多条件查询教室
    // 根据请求参数搜索教室
    @GetMapping("/searchClassrooms")
    public String searchClassrooms(
            // 教室名称
            @RequestParam(required = false) String classroomName,
            // 容量
            @RequestParam(required = false) String capacity,
            // 状态
            @RequestParam(required = false) String status,
            // 地点
            @RequestParam(required = false) String building,
            // 描述
            @RequestParam(required = false) String description,
            Model model) {
        // 调用服务层搜索教室
        List<Classrooms> classroomList = classroomService.searchClassrooms(classroomName, capacity, status, building, description);
        // 打印获取到的数据
        System.out.println("获取到的数据"+classroomList);
        // 将搜索结果添加到模型中
        model.addAttribute("classroomList", classroomList);
        // 返回教室页面
        return "classroom";
    }


    //查看用户预约记录
    @RequestMapping(value = "/myReservations", method = RequestMethod.GET)
    public String myReservations(Model model, HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        if (user != null) {
            List<Reservations> reservationsList = reservationService.findAllReservations();
            model.addAttribute("reservationsList", reservationsList);
            return "myReservations";
        } else {
            model.addAttribute("error", "请先登录");
            return "redirect:/login";
        }
    }


    //进入个人中心,获取当前用户信息
//    @RequestMapping(value = "/PersonalCenter", method = RequestMethod.GET)
//    public String PersonalCenter(Model model, HttpSession session) {
//        Users user = (Users) session.getAttribute("user");
//        System.out.println(user);
//        if (user != null) {
//            model.addAttribute("user", user);
//            return "individualistic";
//        } else {
//            return "redirect:/login";
//        }
//    }
    @RequestMapping(value = "/PersonalCenter", method = RequestMethod.GET)
    public String personalCenter(Model model, HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        if (user != null) {
            model.addAttribute("user", userService.getUserById(user.getUserId())); // 从数据库获取最新的用户信息
            return "individualistic";
        } else {
            return "redirect:/login";
        }
    }

    //修改密码
    @RequestMapping(value = "/updatePhone", method = RequestMethod.POST)
    public String updatePhone(@RequestParam("newPhoneNumber") String phoneNumber, Model model, HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        if (user != null) {
            boolean success = userService.updatePhoneNumber(user.getUserId(), phoneNumber);
            if (success) {
                user.setPhoneNumber(phoneNumber);
                session.setAttribute("user", user); // 更新 session 中的用户信息
                model.addAttribute("user", user);
                model.addAttribute("success", "手机号码更新成功");
            } else {
                model.addAttribute("error", "手机号码更新失败");
            }
        } else {
            model.addAttribute("error", "请先登录");
        }
        return "redirect:/user/PersonalCenter"; // 重定向到个人中心页面
    }


    //修改手机号
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public String updatePassword(@RequestParam("newPassword") String newPassword, Model model, HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        if (user != null) {
            boolean success = userService.updatePassword(user.getUserId(), newPassword);
            if (success) {
                user.setPhoneNumber(newPassword);
                session.setAttribute("user", user); // 更新 session 中的用户信息
                model.addAttribute("user", user);
                model.addAttribute("success", "手机号码更新成功");
            } else {
                model.addAttribute("error", "手机号码更新失败");
            }
        } else {
            model.addAttribute("error", "请先登录");
        }
        return "redirect:/user/PersonalCenter"; // 重定向到个人中心页面
    }

    @RequestMapping(value = "/updatePassword", method = RequestMethod.GET)
    public String showUpdatePasswordPage(Model model, HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        model.addAttribute("user", user);
        return "changePassword";
    }


    @RequestMapping(value = "/updatePhone", method = RequestMethod.GET)
    public String showUpdatePhonePage(Model model, HttpSession session) {
        Users users = (Users) session.getAttribute("user");
        model.addAttribute("user", users);
        return "changePhoneNumber";
    }


}


